package com.hulk.summary;


import com.hulk.summary.annotation.EnableSummaryConfig;
import com.hulk.summary.constant.APICons;
import com.hulk.summary.filter.SummaryFilter;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 *  configuration
 *
 * @author hulk
 */
@Configuration
@ConditionalOnBean(annotation = EnableSummaryConfig.class)
@AllArgsConstructor
public class SummaryAutoConfiguration {

    @Autowired
    public SummaryProperties properties;

    /**
     * xss过滤拦截器
     */
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new SummaryFilter(properties));
        filterRegistrationBean.setOrder(properties.getOrder());
        filterRegistrationBean.setEnabled(properties.isEnabled());
        filterRegistrationBean.addUrlPatterns(properties.getIncludePaths());

        Map<String, String> initParameters = new HashMap<>();
        initParameters.put(APICons.INCLUDE_PATHS, properties.getIncludePaths());
        initParameters.put(APICons.EXCLUDE_PATHS, properties.getExcludePaths());
        initParameters.put(APICons.EXCLUDE_FIELDS,properties.getExcludeFields());
        filterRegistrationBean.setInitParameters(initParameters);


        return filterRegistrationBean;
    }


}
